home *** CD-ROM | disk | FTP | other *** search
- // Copyright (C) 1997-2002 Alias|Wavefront,
- // a division of Silicon Graphics Limited.
- //
- // The information in this file is provided for the exclusive use of the
- // licensees of Alias|Wavefront. Such users have the right to use, modify,
- // and incorporate this code into other products for purposes authorized
- // by the Alias|Wavefront license agreement, without fee.
- //
- // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
- // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
- // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
- // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- //
- //
- // Alias|Wavefront Script File
- // MODIFY THIS AT YOUR OWN RISK
- // Creation Date: Sept 22, 1999
- //<doc>
- //<name basename>
- //<owner "Alias|Wavefront Unsupported">
- //
- //<synopsis>
- // basename(string $path, string $extension)
- //
- //<description>
- // Strips from $path any prefix ending in '/'. If $path ends in a '/'
- // it is first stripped. If $suffix is not empty, $suffix will also be
- // stripped if it exists at the end of $path. On NT, the input $path is
- // first processed to convert all backslashes ('\', input as "\\") to '/'s.
- //
- //<flags>
- // string $path Path string to strip
- // string $suffix Suffix to strip off
- //
- //<returns>
- // string: the stripped basename
- //
- //<examples>
- // string $path = "/usr/people/gamera/gamera.mel";
- // string $basename = basename( $path, ".mel" );
- // // Result: gamera //
- // basename( $path, "" );
- // // Result: gamera.mel //
- // string $ntPath = "C:/aw/godzilla/hello.c";
- // basename( $ntPath, ".c" );
- // // Result: hello //
- //
- //</doc>
- //
- global proc string basename( string $path, string $suffix )
- {
- if ( `about -nt` || `about -mac`)
- $path = convert( $path );
-
- // Check to make sure we're not chopping off slash if
- // it's the only path delimiter!
- if ( (match( "^/$", $path ) == "") && // Root dir
- (match( "^[A-Za-z]:/$", $path ) == "" ) ) // X:/ on NT
- {
- // Strip off last '/', if it exists
- $path = substitute("/$", $path, "");
- }
-
- string $basename;
- // Is this a UNC path?
- if ( match( "^//", $path ) != "" )
- {
- // If so, we check if there is anything after the first
- // component of the path. If not, we return nothing.
- if ( match( "^//.*/.*", $path ) != "" )
- $basename = match( "[^/]*$", $path );
- else
- return "";
- }
- else // We get everything after the last "/"
- $basename = match("[^/]*$", $path);
-
- // if $suffix is given, replace it (if it's at the end) with ""
- if ( size( $suffix ) != 0 )
- {
- $suffix = $suffix + "$";
- $basename = substitute( $suffix, $basename, "" );
- }
-
- return $basename;
- }
-